package com.persondemo.videoappliction.ui.vip;

import android.Manifest;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.persondemo.videoappliction.R;
import com.persondemo.videoappliction.bean.VipBean;
import com.persondemo.videoappliction.component.ApplicationComponent;
import com.persondemo.videoappliction.component.DaggerHttpComponent;
import com.persondemo.videoappliction.event.MainEvent;
import com.persondemo.videoappliction.runtimepermissions.PermissionsManager;
import com.persondemo.videoappliction.runtimepermissions.PermissionsResultAction;
import com.persondemo.videoappliction.ui.base.BaseActivity;
import com.persondemo.videoappliction.ui.base.BaseFragment;
import com.persondemo.videoappliction.ui.vip.contract.VipContract;
import com.persondemo.videoappliction.ui.vip.presenter.VipPresenter;
import com.persondemo.videoappliction.utils.ImageLoaderUtil;
import com.persondemo.videoappliction.utils.RxDeviceTool;
import com.persondemo.videoappliction.utils.T;

import org.simple.eventbus.Subscriber;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/3/22.
 * vip推广
 */

public class VipActivity extends BaseActivity<VipPresenter> implements VipContract.View {
    @BindView(R.id.vip_img)
    ImageView vipImg;
    Unbinder unbinder;
    @BindView(R.id.vip_liner)
    LinearLayout vipLiner;
    VipBean vip = new VipBean();
    @BindView(R.id.vip_titile)
    TextView vipTitile;
    @BindView(R.id.vip_name)
    TextView vipName;
    @BindView(R.id.vip_scroll)
    ScrollView vipScroll;
    String imei = "";




    @Override
    public int getContentLayout() {
        return R.layout.module_frament_vip;
    }

    @Override
    public void initInjector(ApplicationComponent appComponent) {
        DaggerHttpComponent.builder()
                .applicationComponent(appComponent)
                .build()
                .inject(this);
    }

    @Override
    public void bindView(View view, Bundle savedInstanceState) {
        vipScroll.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        vipLiner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (vip != null)
                    if (!TextUtils.isEmpty(vip.getData().getUserurl())){
                        copy(vip.getData().getUserurl());
                    }else {
                        T.showShort(VipActivity.this,"复制失败！");
                    }

            }
        });

    }

    @Subscriber
    private void godata(MainEvent event) {
        if (event == null) return;
        String title = event.title;
        T.showShort(VipActivity.this, "vip" + title);
    }

    private void check() {
        PermissionsManager.getInstance().requestPermissionsIfNecessaryForResult(VipActivity.this, new String[]{
                Manifest.permission.READ_PHONE_STATE
        }, new PermissionsResultAction() {
            @Override
            public void onGranted() {
                imei = RxDeviceTool.getIMEI(VipActivity.this);
                mPresenter.getgeata(imei);
                Log.e("lidaming00", "iemi:" + imei);
            }

            @Override
            public void onDenied(String permission) {
                T.showShort(VipActivity.this, "用户拒绝授权");
            }
        });
    }

    @Override
    public void initData() {
        if (mPresenter != null) {
            check();
            if (TextUtils.isEmpty(imei)) {
                mPresenter.getgeata(imei);
                Log.e("lidaming01", "iemi:" + imei);
            }
        }
    }


    private void copy(String title) {
        // 从API11开始android推荐使用android.content.ClipboardManager
        // 为了兼容低版本我们这里使用旧版的android.text.ClipboardManager，虽然提示deprecated，但不影响使用。
        //获取剪贴板管理器：
        ClipboardManager cm = (ClipboardManager) (VipActivity.this).getSystemService(Context.CLIPBOARD_SERVICE);
// 创建普通字符型ClipData
        ClipData mClipData = ClipData.newRawUri("Label", Uri.parse(title));
// 将ClipData内容放到系统剪贴板里。
        cm.setPrimaryClip(mClipData);
        T.showShort(VipActivity.this, "专属链接复制成功！");
    }

    @Override
    public void showdata(VipBean vipBean) {
        if (vipBean == null) {
            showFaild();
            return;
        }
        if (vipBean.getStatus() == 1) {
            ImageLoaderUtil.LoadImage(VipActivity.this, vipBean.getData().getQcode(), vipImg);
            vipTitile.setText(vipBean.getData().getUserurl());
            vipName.setText("当前用户id:" + vipBean.getData().getUid() + " 邀请人数：" + vipBean.getData().getYaiqing());
            vip = vipBean;
        }
    }

    @Override
    public void showFaild() {
        super.showFaild();
        T.showShort(VipActivity.this, "加载失败！");
    }

    @Override
    public void onRetry() {
        
    }
}
